package com.sgb.kjwl.viewmodel.machine;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.CompanyInvitationEntity;
import com.sgb.kjwl.model.entity.PublishLeaseDataEntity;
import com.sgb.kjwl.view.ui.activity.house.LeaseInviteActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.json.JSONException;
import org.json.JSONObject;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LeaseInviteModel extends BaseViewModel {

    private final LeaseInviteActivity leaseInviteActivity;

    public LeaseInviteModel(LeaseInviteActivity activity) {
        this.leaseInviteActivity = activity;
    }

    //邀请企业
    public void addInvitation(CompanyInvitationEntity entity) {
        DialogHelper.showProgressDialog(leaseInviteActivity, null, "提交数据中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().addinvitation(entity).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                try {//接口返回数据不标准,只能自行转换
                    JSONObject jsonObject = new JSONObject(response.body().getData().toString());
                    int code = jsonObject.getInt("code");
                    String msg = jsonObject.getString("msg");
                    if (code == 0) {
                        MToast.showToast(leaseInviteActivity, "邀请成功");
                        leaseInviteActivity.finish();
                    } else {
//                        MToast.showToast(leaseInviteActivity, entity.getCompanyName() + "已存在");
                        MToast.showToast(leaseInviteActivity, msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    MToast.showToast(leaseInviteActivity, "邀请失败");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(leaseInviteActivity, "邀请失败");
            }
        });
    }

}
